<template>
  <div>
    <el-form class="query-table-form" @submit.native.prevent :inline="true" :model="params">
      <el-form-item>
        <el-input
          v-model="params.customer_name"
          placeholder="项目名称查询"
          clearable
          @change="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.corp_name"
          placeholder="负责人查询"
          clearable
          @change="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <select-address
          v-model="params.address_code"
          placeholder="地址查询"
          auto-top
          clearable
          @change="getData()"
        ></select-address>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="getData()">查询</el-button>
      </el-form-item>
    </el-form>

    <c-table border v-loading="loading" :columns="tableColumns" :data="tableData">
      <template v-slot:address="scope">
        <el-tooltip effect="light" :content="scope.row.address">
          <div class="text-truncate">{{ scope.row.address }}</div>
        </el-tooltip>
      </template>

      <template v-slot:projectEleStatus="scope">
        <span class="badge badge-wrap" :class="`badge-${scope.row.projectEleStatusStyle}`">
          {{ scope.row.projectEleStatusLabel }}
        </span>
      </template>
    </c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <el-dialog
      :visible.sync="reportVisible"
      :close-on-click-modal="false"
      :before-close="beforeClose"
      append-to-body
      top="30px"
      width="80%"
    >
      <el-tabs v-model="activeName">
        <el-tab-pane label="签约登记" name="file">
          <contract-registr :query-id="queryId" view-mode></contract-registr>
        </el-tab-pane>
        <el-tab-pane label="合同审批">
          <contract-approve :query-id="queryId" view-mode></contract-approve>
        </el-tab-pane>
        <el-tab-pane label="合同附件" name="contract">
          <contract-guard-view :query-id="queryId"></contract-guard-view>
        </el-tab-pane>
        <el-tab-pane label="调查报告">
          <credit-file :query-id="queryId" view-mode></credit-file>
        </el-tab-pane>
        <el-tab-pane label="政府推荐函" name="recommend" v-if="fileData.isGovSubmit">
          <recommend-file :related-data="fileData"></recommend-file>
        </el-tab-pane>
        <el-tab-pane label="附件">
          <attach-view :queryId="queryId" :visible="reportVisible"></attach-view>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>

    <attach-file :customerRelatedId="attachId" :visible.sync="attachVisible"></attach-file>

    <contract-registr-dialog
      :visible.sync="editVisible"
      @success="handleSuccess()"
      :query-id="editId"
    ></contract-registr-dialog>
  </div>
</template>

<script>
import SelectAddress from '@/components/select/SelectAddress'
import CreditFile from '@/credit/components/creditFile/InvestigateReport'
import RecommendFile from '@/credit/views/city/program/RecommendFile'
import AttachFile from '@/credit/views/city/program/Attach'
import AttachView from '@/credit/components/AttachView'
import taskTableMixin from '@/credit/mixin/self/taskTable'

import { enumGuatype } from '@/credit/plugins/enumTown'
import ContractRegistrDialog from '@/credit/views/city/self/components/contractRegistr/ContractRegistrDialog'
import ContractRegistr from '@/credit/views/city/self/components/contractRegistr/ContractRegistr'
import ContractApprove from '@/credit/views/city/self/components/contractApprove/Main'
import ContractGuardView from '@/credit/views/county/unionProvince/components/ContractGuardView'
export default {
  components: {
    SelectAddress,
    CreditFile,
    RecommendFile,
    AttachFile,
    AttachView,
    ContractRegistrDialog,
    ContractRegistr,
    ContractApprove,
    ContractGuardView,
  },
  mixins: [taskTableMixin],
  props: {},
  data() {
    return {
      params: {
        index: 1,
        size: 10,
        customer_name: '',
        corp_name: '',
        address_code: '',
        'gua_type.eq': 2,
        'contract_approval_status.eq': 2,
        'confuse_status.neq': 3,
        subKey: 'contract_approval.customer_related_id',
      },
      order: {
        confuse_time: 'DESC',
      },
      useRegisterTable: true,
      tableColumns: [
        {
          attrs: { label: '主体名称', prop: 'customer_name', minWidth: '120px' },
          process: (v, row) => ({
            attrs: { class: 'project-history', attrs: { 'data-history': row.is_history } },
          }),
        },
        { attrs: { label: '业务类型', prop: 'guaType', align: 'center' } },
        { attrs: { label: '地址', prop: 'address' }, slot: 'address' },
        { attrs: { label: '负责人', prop: 'corp_name', align: 'center' } },
        { attrs: { label: '联系电话', prop: 'mobile_no', align: 'center' } },
        // { attrs: { label: '申请金额(万元)', prop: 'apply_funds', align: 'center' } },
        // { attrs: { label: '审批完成时间', prop: 'approveDate', align: 'center', width: '100' } },
        { attrs: { label: '合作银行', prop: 'bank_name', align: 'center' } },
        { attrs: { label: '待处理人员', prop: 'res_person_risk_point', align: 'center' } },
        {
          attrs: { label: '操作', align: 'center', minWidth: 150 },
          operations: [
            {
              content: '签约登记',
              icon: 'fa fa-gavel',
              event: this.handleEdit,
              ifRender: row => row.editable,
            },
            // {
            //   content: '附件',
            //   icon: 'fa fa-clipboard',
            //   event: this.handleAttach,
            // },
            {
              content: '查看',
              icon: 'fa fa-search',
              event: this.hanleView,
            },
          ],
        },
      ],

      activeName: 'file',

      // 档案卡
      queryId: '',
      reportVisible: false,
      fileData: {},

      // 附件
      attachId: '',
      attachVisible: false,

      editVisible: false,
      editId: '',
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getData()
  },
  mounted() {},
  methods: {
    dataAdapter(item) {
      item.guaType = enumGuatype[item.gua_type] || '-'
      item.refuseTime = this.$utils.dateFormat(item.confuse_time, 'yyyy-MM-dd')

      item.submitType = +item.submit_type
      item.isGovSubmit = item.submitType === 1
      const approval = item.contract_approval?.[0] || {}
      item.approveDate = this.$utils.dateFormat(approval.res_corp_date, this.dateFmt)
      item.editable = +approval.is_online_sign === 0 && item.res_user_id_risk_point === this.userId
      return item
    },
    handleAttach(row) {
      this.attachId = row.customer_related_id
      this.attachVisible = true
    },
    handleEdit(row = {}) {
      this.editId = row.customer_related_id
      this.editVisible = true
    },
    hanleView(row) {
      this.showFile(row)
    },
    showFile(row = {}) {
      this.fileData = row
      this.queryId = this.fileData.customer_related_id
      this.reportVisible = true
    },
    beforeClose() {
      this.reportVisible = false
      setTimeout(() => (this.activeName = 'file'), 200)
    },
    handleSuccess() {
      this.$store.dispatch('getTaskCount')
      this.getData()
    },
  },
}
</script>
<style lang="scss" scoped></style>
